
#Write latex tables with notes:
write_latex = function(star_table, note_text, out_path, scale) {
  
  table_note = note_text %>% 
    str_replace_all("\n", " ")
  
  preamble = c( "\\scalebox{", scale, "}{ 
                \\begin{threeparttable}")
  
  out_table = append(star_table, 
                     preamble,
                     str_detect(star_table, "centering") %>% which) %>%
    append(.,
           c("\\begin{tablenotes}", "\\small", 
             paste0("\\item \\textit{Note}: $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01.", table_note), 
             "\\end{tablenotes}", "\\end{threeparttable}}"),
           str_detect(., "[\\\\]end\\{tabular\\}") %>% which)
  
  cat(out_table, sep = '\n', file = out_path)
}

#Write latex tables with notes:
write_latex_plain = function(star_table, note_text, out_path) {
  
  cat(star_table, sep = '\n', file = out_path)
}


#Write latex tables with notes:
write_latex_placebo = function(star_table, note_text, out_path, scale) {
  
  table_note = note_text %>% 
    str_replace_all("\n", " ")
  
  preamble = c( "\\scalebox{", scale, "}{ 
                \\begin{threeparttable}")
  
  out_table = append(star_table, 
                     preamble,
                     str_detect(star_table, "centering") %>% which) %>%
    append(.,
           c("\\begin{tablenotes}", "\\small", 
             paste0("\\item \\textit{Note}: $^{*}$p$<$0.01; $^{**}$p$<$0.05; $^{***}$p$<$0.01. ", table_note), 
             "\\end{tablenotes}", "\\end{threeparttable}}"),
           str_detect(., "[\\\\]end\\{tabular\\}") %>% which)
  
  cat(out_table, sep = '\n', file = out_path)
}


loess_plot <- function(x, y, W, ylab, scale = F){
  
  ##making the loess fit
  tmp <- na.omit(data.frame(x = x, y = y, W = W))
  #tmp <- tmp[tmp$x > -0.30 & tmp$x < 0.30,]
  loess_reg <- loess(y ~ x * W, data = tmp, degree = 1)
  tmp$y_fitted <- fitted.values(loess_reg)
  tmp$se <- predict(loess_reg, tmp, se = TRUE)$se.fit
  
  
  #making plots
  p <- ggplot(data = tmp, aes(x = x, y = y)) +
    geom_ribbon(aes(ymin = y_fitted - 1.67 * se, ymax = y_fitted + 1.67 * se, group = W), alpha = .2, linetype = "dashed", color="darkgrey") +
    geom_line(aes(y = y_fitted, group = W), color = 'black') + 
    theme_bw() + 
    xlab("Distance to border (meters)") + 
    ylab(ylab) + 
    scale_x_continuous(breaks = seq(-15000, 15000, 5000)) +
    theme(text=element_text(size=10, family="Times")) +
    theme(panel.grid.minor = element_blank(), panel.grid.major.x = element_blank(),
          axis.line = element_line(colour = "black"),
          panel.border = element_blank()) + 
    geom_vline(xintercept = 0, color = "black", linetype = "dashed") +
    stat_summary_bin(fun.y='mean', bins=18,
                     color='black', size=2, geom='point', alpha = 0.4)
  
  if(scale == T){
    p + 
      ylim(0, 1)
    return(p)
  }else{
    return(p)
  }
  
}

make_opium_plot <- function(df, var, bw, label){
  
  var = enquo(var)
  
  
  df %<>% 
    filter(abs(dist) < bw) %>%
    mutate(treat = (forcing > 0)*1) %>%
    mutate(outcome = !!var) %>%
    mutate(bins = cut(forcing, breaks = seq(from = (0-bw), to = (0+bw), by=1666.667), dig.lab = 6)) %>%
    group_by(bins) %>%
    mutate(n_obs = n()) %>%
    mutate(outcome_binned = mean(outcome, na.rm = T)) %>%
    mutate(bins_split = bins,
           bins_split = str_replace_all(bins_split, "\\(|\\)|\\[|\\]", "")) %>%
    separate(bins_split, into = c("start_bin", "end_bin"), sep = ",") %>%
    mutate_at(vars(start_bin, end_bin), funs(as.numeric(.))) %>%
    mutate(bin_position = (start_bin + end_bin)/2)
  
  bin_df = 
    df %>% 
    dplyr::select(bin_position, outcome_binned, n_obs) %>% 
    distinct() %>% 
    mutate(treat = NA_real_)
  
  plot <-
    df %>%
    ggplot(aes(x = forcing, y = outcome, group = treat)) + 
    geom_vline(aes(xintercept = 0), color = 'black', size = 0.5, linetype = 'dashed') + 
    geom_smooth(method = "lm", color = "black",size = 0.4) +
    theme_bw() +
    geom_point(data = bin_df, aes(x=bin_position, y = outcome_binned, size = n_obs), color = "black", fill = alpha("lightgrey", 0.4), shape = 21, stroke = 0.75) +
    theme(panel.grid.minor = element_blank(), 
          panel.grid.major.x = element_blank(),
          axis.line = element_line(colour = "black"),
          panel.border = element_blank()) +
    xlab("Distance to border (meters)") + 
    scale_x_continuous(breaks = seq(-15000, 15000, 5000)) +
    coord_cartesian(ylim = c(0, 1)) + 
    ylab("Pr(Intolerant = Yes)") +
    theme(legend.position = "none")
  
  return(plot)
}


loess_plot_weighted <- function(x, y, W, ylab, weights, bin_position, bin_val){
  
  
  ##making the loess fit
  tmp <- na.omit(data.frame(x = x, y = y, W = W, weights = weights))
  #tmp <- tmp[tmp$x > -0.30 & tmp$x < 0.30,]
  loess_reg <- loess(y ~ x * W, weights = weights, data = tmp, degree = 1)
  tmp$y_fitted <- fitted.values(loess_reg)
  tmp$se <- predict(loess_reg, tmp, se = TRUE)$se.fit
  
  binned =
    data.frame(bin_position = bin_position,
               bin_val = bin_val) %>%
    distinct()

  
  #making plots
  p <- ggplot(data = tmp, aes(x = x, y = y)) +
    geom_ribbon(aes(ymin = y_fitted - 1.67 * se, ymax = y_fitted + 1.67 * se, group = W), alpha = .2, linetype = "dashed", color="darkgrey") +
    geom_line(aes(y = y_fitted, group = W), color = 'black') + 
    theme_bw() + 
    xlab("Distance to border (meters)") + 
    ylab(ylab) + 
    scale_x_continuous(breaks = seq(-15000, 15000, 5000)) +
    theme(text=element_text(size=10, family="Times")) +
    theme(panel.grid.minor = element_blank(), panel.grid.major.x = element_blank(),
          axis.line = element_line(colour = "black"),
          panel.border = element_blank()) + 
    geom_vline(xintercept = 0, color = "black", linetype = "dashed") +
    geom_point(data = binned, aes(x=bin_position, y = bin_val), color = "black", size = 2, alpha = 0.4)
  
  return(p)
}




mod_plot <- function(x, y, d, ylab, xlab, title){
  
  
  tmp <- na.omit(data.frame(x = x, y = y, d = d))
  
  b0 <- summary(lm(y ~ x, data = tmp[tmp$d == 0,]))[[4]][2,1]
  se0 <- summary(lm(y ~ x, data = tmp[tmp$d == 0,]))[[4]][2,2]
  
  b1 <- summary(lm(y ~ x, data = tmp[tmp$d == 1,]))[[4]][2,1]
  se1 <- summary(lm(y ~ x, data = tmp[tmp$d == 1,]))[[4]][2,2]
  
  tmp = cbind(c(b0, b1), c(se0, se1), c("No", "Yes")) %>% 
    data.frame()
  
  tmp$X1 = as.numeric(as.character(tmp$X1))
  tmp$X2 = as.numeric(as.character(tmp$X2))
  
  
  ggplot(tmp, aes(x=factor(X3), y = as.numeric(X1), group = 1)) + 
    geom_point(color = "black", fill = "white", size = 2) + 
    geom_errorbar(aes(ymin=X1 - 1.96*as.numeric(X2), ymax = X1 + 1.96*as.numeric(X2)),
                  width = 0.075, color = "red", position=position_dodge(width=0.5)) +
    theme_minimal() + 
    geom_hline(yintercept = 0, linetype = "dashed", color = "black") + 
    geom_line(alpha = 0.5) + 
    xlab(xlab) + 
    ylab(ylab) +
    ggtitle(title)+
    theme(plot.title = element_text(hjust = 0.5)) +
    theme(text=element_text(size=12, family="Times")) %>%
    return()
  
  
}

